﻿using System.Collections.ObjectModel;
using System.Windows.Input;
using System.Windows.Media.Imaging;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MusicExplorer.Data;
using MusicExplorer.Resources.Images;
using MusicExplorer.View.Popup;
using MusicExplorer.ViewModel;
using MusicExplorer.ViewModel.Popup;
using MusicExplorer.WebRepository.Service;
using System.Linq;

namespace MusicExplorer.Wrappers
{
    public class WebRepositoryGroupWrapper
    {
        private readonly ObservableCollection<WebRepositoryWrapper> m_webRepositories;

        private readonly REF_TYPE_WEB_REPOSITORY m_typeWebRepositories;
        public REF_TYPE_WEB_REPOSITORY TypeWebRepositories { get { return m_typeWebRepositories; } }

        private readonly MainWindowViewModel m_parent;

        public WebRepositoryGroupWrapper(MainWindowViewModel parent, REF_TYPE_WEB_REPOSITORY typeWebRepositories)
        {
            m_parent = parent;
            m_typeWebRepositories = typeWebRepositories;
            m_webRepositories = new ObservableCollection<WebRepositoryWrapper>();
        }

        public string DisplayName { get { return m_typeWebRepositories.type_web_repo_libelle; } }

        public ObservableCollection<WebRepositoryWrapper> WebRepositories { get { return m_webRepositories; } }

        public BitmapImage DisplayIcon { get { return Images.GetIcon(m_typeWebRepositories.type_web_repo_key); } }

        public ICommand ManageWebRepositories
        {
            get
            {
                return new RelayCommand(() =>
                {
                    EntityPreferences preferences = null;
                    switch (m_typeWebRepositories.type_web_repo_key)
                    {
                        case "YoutubeChannel":
                            preferences = new EntityPreferences() { DataContext = new YoutubeWebRepositoryPreferencesViewModel(m_parent, this) };
                            break;
                        case "SoundcloudAccount":
                            preferences = new EntityPreferences() { DataContext = new SoundcloudWebRepositoryPreferencesViewModel(m_parent, this) };
                            break;
                        default:
                            preferences = new EntityPreferences() { DataContext = new WebRepositoryGroupPreferencesViewModel(m_parent, this) };
                            break;
                    }

                    var result = preferences.ShowDialog();
                    if (result.HasValue && result.Value)
                    {
                        var dataContext = preferences.DataContext as WebRepositoryGroupPreferencesViewModel;
                        dataContext.UpdateEntities();
                        ServiceWebRepository.SaveWebRepositoriesPreferences(m_typeWebRepositories, dataContext.Entities.Select(w => w.Entity as MusicExplorer.Data.WEB_REPOSITORY));
                        m_parent.WebRepositoryTree.RefreshTree();
                    }
                });
            }
        }
    }
}
